<template>
  <div id="app">
    <div class="container">
      <div class="close"><span class="iconfont iconicon-test"></span></div>
      <div class="logo"><span class="iconfont iconnew"></span></div>
      <div class="inputs">
        <!-- v-model实现两个操作：
        1.为子组件的value属性赋值
        2.监听子组件所发出的input事件-->
        <myinput
          placeholder="请输入用户名/手机号"
          v-model="user.username"
          :rules='/^1\d{4}$|^1[35789]\d{9}$/'
          msg='请输入5位或者11位的手机号'
        ></myinput>
        <myinput placeholder="密码" class="input" type="password" v-model="user.password"
        :rules='/^.{3,16}$/'></myinput>
      </div>
      <p class="tips">
        没有账号？
        <a href="#/register" class="">去注册</a>
      </p>
      <mybutton @click="login" type="primary">登陆</mybutton>
      <!-- <van-button type="primary">主要按钮</van-button> -->
    </div>
  </div>
</template>

<script>
import mybutton from "@/components/mybutton.vue";
import myinput from "@/components/myinput.vue";
// import { Button } from 'vant'
import { login } from '@/apis/user.js'
export default {
  data() {
    return {
      user: {
        username: '10086',
        password: '123',
      },
      show: false,
    };
  },
  components: {
    mybutton,
    myinput,
    // 'van-button':Button,
  },
  methods: {
    async login() {
      console.log("login");
      if(/^(\d{5,6})$|^(1\d{10})$/.test(this.user.username) && /^\S{3,16}$/.test(this.user.password)){
        let res = await login(this.user)
        console.log(res);
        if(res.data.message == '用户不存在'){
          // 给用户提示
          this.$toast.fail(res.data.message)
        }else{
          this.$toast.success(res.data.message)
          localStorage.setItem('hm_toutiao',res.data.data.token)
           // 本地存储只能存储字符串，如果直接存储对象，就会默认调用对象的toString方法，从而存储了[object,Object]
          localStorage.setItem('hm_toutiao_userinfo',JSON.stringify(res.data.data))
          let url = decodeURIComponent(window.location.hash.split('=')[1])
          console.log(url);
          // 跳转到适合的页面
          if (url){
            this.$router.push({path:url})
          }else{
            this.$router.push({path:`/personal/${res.data.data.user.id}`})  
          }
        }
      }else{
        this.$toast.fail('用户数据输入不合法')
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}


.close {
  span {
    font-size: 27 / 360 * 100vw;
  }
}


.logo {
  display: flex;
  justify-content: center;


  span {
    display: block;
    font-size: 126 / 360 * 100vw;
    color: #d81e06;
  }
}


.inputs {
  input {
    margin-bottom: 20px;
  }
}


.tips {
  text-align: right;
  margin-bottom: 20px;


  a {
    color: #3385ff;
  }
}
</style>